package com.gab.controller;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.gab.pojo.TbUser;
import com.gab.service.TbUserService;
import com.gab.utils.PageResult;
import com.gab.utils.Result;
import com.gab.utils.SendEmail;


/**
 * controller
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/user")
public class TbUserController {

	@Autowired
	private TbUserService tbUserService;
	@Autowired
	SendEmail sm;
	
	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<TbUser> findAll(){			
		return tbUserService.findAll();
	}
	
	
	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult  findPage(int page,int rows){			
		return tbUserService.findPage(page, rows);
	}
	
	/**
	 * 增加
	 * @param advantage
	 * @return
	 */
	@RequestMapping(
		    value="/add",
		    method=RequestMethod.POST,
		    consumes="application/json")
	public Result add(@RequestBody TbUser btUser){
		
		String formEmail="dimash_diko@163.com";
		//多个收件人用逗号，隔开
		String toEmail="1337440758@qq.com,cxw372@qq.com";
		
		String companyname=btUser.getCompanyname();
		String name=btUser.getName();
		String phone=btUser.getPhone();
		String wechat=btUser.getWechat();
		String type=btUser.getType();
		String message=btUser.getMessage();
		System.out.println("the user is:"+btUser.getCompanyname());
		System.out.println("the user is:"+btUser.getName());
		System.out.println("the user is:"+btUser.getPhone());
		System.out.println("the user is:"+btUser.getWechat());
		System.out.println("the user is:"+btUser.getType());
		System.out.println("the user is:"+btUser.getMessage());
		//使用java发送邮件5步骤
		//1.创建sesssion
		Properties prop=new Properties();
		prop.put("mail.host","smtp.163.com" );
		prop.put("mail.transport.protocol", "smtp");
		prop.put("mail.smtp.auth", true);
		Session session=Session.getInstance(prop);
		//开启session的调试模式，可以查看当前邮件发送状态
		session.setDebug(true);
		try {
			//2.通过session获取Transport对象（发送邮件的核心API）
			Transport ts=session.getTransport();
			//3.通过邮件用户名密码链接
			//邮箱名", "授权码"
			ts.connect("dimash_diko@163.com", "diko15574708702");
		//4.创建邮件
		Message msg=sm.createSimpleMail(session,companyname,name,phone,wechat,type,message, formEmail, toEmail);
		//5.发送电子邮件
		ts.sendMessage(msg, msg.getAllRecipients());
		}catch(Exception e) {
		e.printStackTrace();
		}
		
		
		try {
			tbUserService.add(btUser);
			return new Result(true, "增加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "增加失败");
		}
	}
	
	/**
	 * 修改
	 * @param advantage
	 * @return
	 */
	@RequestMapping("/update")
	public Result update(@RequestBody TbUser tbUser){
		try {
			tbUserService.update(tbUser);
			return new Result(true, "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "修改失败");
		}
	}	
	
	/**
	 * 获取实体
	 * @param id
	 * @return
	 */
	@RequestMapping("/findOne")
	public TbUser findOne(Long id){
		return tbUserService.findOne(id);		
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public Result delete(Long [] ids){
		try {
			tbUserService.delete(ids);
			return new Result(true, "删除成功"); 
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
	
		/**
	 * 查询+分页
	 * @param brand
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/search")
	public PageResult search(@RequestBody TbUser tbUser, int page, int rows  ){
		return tbUserService.findPage(tbUser, page, rows);		
	}
	
}
